import Vue from 'vue'
import Vuex from 'vuex'


Vue.use(Vuex)

const debug = process.env.NODE_ENV !== 'production'

export default new Vuex.Store({
  state: {
    /**
     * 页面标题
     */
    title: '',
    /**
     * 阿里云图片缩略图基地址
     */
    thumbBaseUrl: '',
    /**
     * 阿里云文件基地址
     */
    fileBaseUrl: '',
    user:{}

  },
  mutations: {
    setTitle (state, title) {
      // 变更状态
      state.title = title
      document.title = '静海区国办幼儿园招生报名管理系统'
    },
    setUserPermissions (state, permissions) {
      debug && console.log(permissions)
      _.forEach(permissions, function (value, key) {
        state.user.permissions.push(value.name)
      })
    },
    setThumbBaseUrl (state, thumbBaseUrl) {
      state.thumbBaseUrl = thumbBaseUrl
    },
    setFileBaseUrl (state, fileBaseUrl) {
      state.fileBaseUrl = fileBaseUrl
    },
    setUser(state, user){
      // debug && console.log(user);
      state.user.type = user.type;
      state.user.token = user.api_token;
      state.user.school_id = user.school_id;
      state.user.id = user.id;
      state.user.name = user.name;
      state.user.permissions = user.permissions;
    },
  },
  actions: {
    fetchUser({commit}){
      axios.get('/api/admin/profile', {params: {include: 'profile'}}).then(function (response) {
        debug && console.log('Susscee:', response);
        let user = response.data.data;
        commit('setUser', user);
        Cookie.set('user', {
          name: user.name,
          email: user.email,
          mobile: user.mobile,
          permissions: user.permissions
        });
      }).catch(function (error) {
        // Something happened in setting up the request that triggered an Error
        debug && console.log('Error', error.message);
      });
    },
  },


  strict: debug,
  // plugins: debug ? [createLogger()] : []
})
